<template>
  <TablePanel title="合同管理">
    <div slot="btm">
          <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              @click="handleAdd"
              v-hasPermi="['crm:contract:add']"
          >新增</el-button>
          <el-button
              type="success"
              plain
              icon="el-icon-edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['crm:contract:edit']"
          >修改</el-button>
          <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['crm:contract:remove']"
          >删除</el-button>
          <el-button
              type="warning"
              plain
              icon="el-icon-download"
              @click="handleExport"
              v-hasPermi="['crm:contract:export']"
          >导出</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <div  slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="合同名称：" prop="contractName">
          <el-input
            v-model="queryParams.contractName"
            placeholder="请输入合同名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户：" prop="customerId">
          <el-input
            v-model="queryParams.customerId"
            placeholder="请输入客户"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div slot="content">
    <el-table
      v-loading="loading"
      border
      :height="tableHeight"
      :data="contractList"
      @selection-change="handleSelectionChange">
      <div slot="empty">
        <el-empty :image="noData" :image-size="200">
        </el-empty>
      </div>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractId" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="合同金额" align="center" prop="money" />
      <el-table-column label="客户" align="center" prop="customerId" />
      <el-table-column label="联系人" align="center" prop="contactsId" />
      <el-table-column label="商机" align="center" prop="businessId" />
      <el-table-column label="下单日期" align="center" prop="orderDate" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="负责人" align="center" prop="ownerUserId" />
      <el-table-column label="创建时间" align="center" prop="createdTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:contract:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:contract:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
      <!-- 添加或修改合同对话框 -->
      <st-dialog :title="title" v-if="open" @close="cancel"
                 padding="100px 0 250px"
                 @save="submitForm"  append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="合同名称:" prop="contractName">
            <el-input v-model="form.contractName" placeholder="请输入合同名称" />
          </el-form-item>
          <el-form-item label="合同金额:" prop="money">
            <el-input v-model="form.money" placeholder="请输入合同金额" />
          </el-form-item>
          <el-form-item label="客户:" prop="customerId">
            <el-input v-model="form.customerId" placeholder="请输入客户" />
          </el-form-item>
          <el-form-item label="联系人:" prop="contactsId">
            <el-input v-model="form.contactsId" placeholder="请输入联系人" />
          </el-form-item>
          <el-form-item label="商机:" prop="businessId">
            <el-input v-model="form.businessId" placeholder="请输入商机" />
          </el-form-item>
          <el-form-item label="合同类型:" prop="types">
            <el-input v-model="form.types" placeholder="请输入合同类型" />
          </el-form-item>
          <el-form-item label="下单日期:" prop="orderDate">
            <el-input v-model="form.orderDate" placeholder="请输入下单日期" />
          </el-form-item>
          <el-form-item label="开始时间:" prop="startTime">
            <el-input v-model="form.startTime" placeholder="请输入开始时间" />
          </el-form-item>
          <el-form-item label="结束时间:" prop="endTime">
            <el-input v-model="form.endTime" placeholder="请输入结束时间" />
          </el-form-item>
          <el-form-item label="备注：" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
      </st-dialog>
  </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

  </TablePanel>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/crm/contract";
import noData from '@/assets/img/no_data.png'
export default {
  name: "Contract",
  data() {
    return {
      noData:noData,
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractName: null,
        customerId: null,
        businessId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(() => {
          this.tableHeight = this.getTableHeightWithTitle()
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        contractName: null,
        money: null,
        customerId: null,
        contactsId: null,
        businessId: null,
        types: null,
        orderDate: null,
        startTime: null,
        endTime: null,
        checkStatus: null,
        examineRecordId: null,
        discountRate: null,
        totalPrice: null,
        paymentType: null,
        roUserId: null,
        rwUserId: null,
        remark: null,
        updatedBy: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractId = row.contractId || this.ids
      getContract(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除合同编号为"' + contractIds + '"的数据项？').then(function() {
        return delContract(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
